package com.mdxl.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局读取session存储的相关信息
 */
public class SessionManager {
    RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();

    private Object getSessionAttr(String name) {

        Object obj = null;
        if (!name.trim().equals("")) {
            obj = request.getSession().getAttribute(name);
        }

        return obj;
    }

    private String getRequestURL() {
        return request.getRequestURL().toString();
    }

    public static String getUserID() {

        Object obj = (new SessionManager()).getSessionAttr("userId");
        if (obj == null) return null;
        return obj.toString();
    }

    public static String getSiteID() {

        Object obj = (new SessionManager()).getSessionAttr("orgId");
        if (obj == null) return null;
        return obj.toString();
    }

    public static String getRefere() {
        Object obj = (new SessionManager()).getSessionAttr("referer");
        if (obj == null) return null;
        return obj.toString();
    }

    public static String getSiteId() {
        Object obj = (new SessionManager()).getSessionAttr("siteId");
        if (obj == null) return null;
        return obj.toString();
    }

    public static Object getCrmUserService() {
        Object obj = (new SessionManager()).getSessionAttr("crmUserService");
        if (obj == null) return null;
        return obj;
    }

    public static Object getProcessEngine() {
        Object obj = (new SessionManager()).getSessionAttr("processEngine");
        if (obj == null) return null;
        return obj;
    }

    public static Object getGainUrlDataService() {
        Object obj = (new SessionManager()).getSessionAttr("gainUrlDataService");
        if (obj == null) return null;
        return obj;
    }

    public static Object getIdentityService() {
        Object obj = (new SessionManager()).getSessionAttr("identityService");
        if (obj == null) return null;
        return obj;
    }

    public void setSessionAttr(String name, Object value) {
        request.getSession().setAttribute(name, value);
    }

    public static String getURL() {
        return (new SessionManager()).getRequestURL();
    }
}